<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="服务商">
          <a-select
            :disabled="display"
            v-decorator="[ 'businessId', {rules: [{ required: true, message: '请选择服务商' }],initialValue: defaultCompany}]"
            placeholder="请选择服务商">
            <a-select-option :value="company.id" v-for="company in companyList" :key="company.id">
              {{company.name}}
            </a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="代理商名称"
        >
          <a-input placeholder="代理商名称"
                   v-decorator="[ 'name',  {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="分润方式">
          <a-radio-group v-decorator="[ 'backType', {rules: [{ required: true, message: '请选择' }],initialValue:0}]">
            <a-radio :value="0">利润</a-radio>
            <a-radio :value="1">固定返点</a-radio>
          </a-radio-group>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="微信："
        >
          <a-input placeholder="微信" v-decorator="[ 'wxRate', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>



        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="支付宝"
        >
          <a-input placeholder="支付宝"  v-decorator="[ 'aliRate', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="服务商名称"-->
<!--        >-->
<!--          <a-input placeholder="服务商名称"  v-decorator="[ 'businessName', {rules: [{ required: true, message: '请输入。。。' }] }]"/>-->
<!--        </a-form-item>-->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save} from '@/api/agentService/agent'
  import { selectAll } from '@/api/businessService/business'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        display:false,
        form: this.$form.createForm(this),
        companyList:[],
        defaultCompany:[],
        isDisabled: false
      }
    },
    //--############  data() ###############-->

    created() {
      selectAll().then(res => {
        this.companyList = res.result
        this.defaultCompany = this.companyList[0].id
        this.company = {
          'companyId': this.defaultCompany,
          'sourceType': 'bigWheel'
        }

      })
    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.display = true
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        this.visible = true
        console.log('00000000',record.backType)
        if(record.backType){
          this.mdl.backType = record.backType
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'name', 'wxRate', 'aliRate','backType','businessId','code','businessName'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl={}
      },

      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id;
            _this.confirmLoading = true
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              }else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
